// pages/items/items.js
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boolHidden: false, //是否显示购物车
    currentTab:1,
    jthjData:[], //家庭环境数据
    totalPrice:0, //家庭环境总价
    totalPrice1:0,//电器家具总价
    totalPrice2:0,//衣物织物总价
    cleaningId:'', //保洁员id
    cleaning:[],  //保洁员信息
    m:''
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync("jthj")
    wx.removeStorageSync("dqjj")
    wx.removeStorageSync("ywzw")
    this.selectComponent("#jthj").getJthjData();
    this.selectComponent("#dqjj").getDqjjData();
    this.selectComponent("#ywzw").getDqjjData();
    this.getCleaning();
    console.log(app.globalData.currenttab)
    if (app.globalData.currenttab) {
      this.setData({
        currentTab: app.globalData.currenttab
      })

    }
    if (app.globalData.cleaner) {
      this.setData({
        cleaningId: app.globalData.cleaner
      })
    }
    if(wx.getStorageSync("address").address==''){
      wx.showModal({
        title: '请先添加服务地址',
        content: '确认添加',
        showCancel:false,
        success:res=>{
          wx.navigateTo({
            url: '/pages/editAddress/editAddress',
          })
        }
      })
    }else{
      this.setData({
        m: wx.getStorageSync("address").m
      })
    }
  },
  onShow(){
    
  },
  backList(){
    wx.removeStorageSync("jthj")
    wx.removeStorageSync("dqjj")
    wx.removeStorageSync("ywzw")
    this.selectComponent("#jthj").getJthjData();
    this.selectComponent("#jthj").removeHours();
    this.selectComponent("#dqjj").getDqjjData();
    this.selectComponent("#ywzw").getDqjjData();
    this.getCleaning();
  },
  getList(){
    this.selectComponent("#header").getAddress()
    .then(res=>{
      this.setData({
        m: res
      })
      this.selectComponent("#jthj").getJthjData();
    })
    
  },
  // 保洁员信息
  getCleaning:function(){
      network.request({
        url: config.host+'api_cleaner/randCleaner',
        method:"POST",
        data:{id:this.data.cleaningId},
        success:res=>{
          console.log("保洁员信息")
          console.log(res)
          if(res.data.code==99){
            this.setData({
              cleaning:res.data.data
            })
            wx.setStorageSync("cleaner", { "data": this.data.cleaning})
          }else{
            wx.showToast({
              title: res.data.msg,
            })
          }
        }
      })
  },
  changeCleaner:function(e){
    this.setData({
      cleaning:e.detail
    })
  },
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  //价格计算

  // 获取家庭环境总价
  totalPrice:function(e){
    console.log(e)
    this.setData({
      totalPrice:e.detail
    })
  },
  //获取电器家具总价
  totalPrice1:function(e){
    this.setData({
      totalPrice1:e.detail
    })
  },
  totalPrice2:function(e){
    this.setData({
      totalPrice2: e.detail
    })
  },
  gotoOrderform(){
    let jthj=''
    let dqjj=''
    let ywzw=''
    let cleaner=this.data.cleaning
    let totalPrice=this.data.totalPrice+this.data.totalPrice1+this.data.totalPrice2
    if (wx.getStorageSync("jthj")){
      jthj=wx.getStorageSync("jthj")
    }
    if (wx.getStorageSync("dqjj")){
       dqjj=wx.getStorageSync("dqjj")
    }
    if (wx.getStorageSync("ywzw")){
       ywzw=wx.getStorageSync("ywzw")
    }
    wx.request({
      url: config.host + 'api_order/order',
      method:"POST",
      data:{jthj:jthj,dqjj:dqjj,ywzw:ywzw,m:this.data.m,totalPrice:totalPrice,cleaner:cleaner},
      success:(res)=>{
        if(res.data.code==99){
          wx.navigateTo({
            url: '/pages/orderform/orderform',
          })
        }else{
          wx.showToast({
            title:res.data.msg,
            icon:'none'
          })
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})